﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;

namespace YJKStairs.AddSigns
{
	internal class StairSingleLinkFileSelectionFilter : ISelectionFilter, ISetNowLevel
	{
		public void SetNowLevel(Level input)
		{
			this.nowLevel = input;
		}

		public void SetTopLevel(Level input)
		{
			this.topLevel = input;
		}

		internal void SetDocument(Autodesk.Revit.DB.Document m_doc)
		{
			this.m_doc = m_doc;
		}

		public bool AllowElement(Element elem)
		{
			this.m_currentInstance = (elem as RevitLinkInstance);
			return this.m_currentInstance != null;
		}

		public bool AllowReference(Reference reference, XYZ position)
		{
			if (this.m_currentInstance == null || this.nowLevel == null || this.m_doc == null)
			{
				return false;
			}
			Document linkDocument = (this.m_doc.GetElement(reference.ElementId) as RevitLinkInstance).GetLinkDocument();
			XYZ realPointForLinkFile = Utility.GetRealPointForLinkFile(this.m_currentInstance, reference.LinkedElementId);
			Element element = linkDocument.GetElement(reference.LinkedElementId);
			double elevation = this.nowLevel.Elevation;
			double num = double.MaxValue;
			if (this.topLevel != null)
			{
				num = this.topLevel.Elevation;
			}
			if (realPointForLinkFile.Z >= elevation && realPointForLinkFile.Z < num)
			{
				if (element is FamilyInstance)
				{
					Family family = (element as FamilyInstance).Symbol.Family;
					for (int i = 0; i <= 1; i++)
					{
                        if (element.Category.Id.IntegerValue == (int)StaticRescours.StairCategories[i] && Regex.IsMatch(family.Name, StaticRescours.FamilyNames[i]))
						{
							return true;
						}
					}
				}
				return false;
			}
			return false;
		}

		private RevitLinkInstance m_currentInstance;

		private Document m_doc;

		private List<Element> selectResturs = new List<Element>();

		private List<int> lstlinkRevitInstanceID = new List<int>();

		private List<Transform> resultsTransforms = new List<Transform>();

		private Level nowLevel;

		private Level topLevel;
	}
}
